/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BreadMain.java
 *
 * Created on 25/04/2011, 03:35:49 م
 */
package com.face;

import com.model.DistributorComboModel;
import com.render.MyCellRenderer;
import com.model.PeopleSearchModel;
import com.model.PeopleTableModel;
import com.print.Reports;
import com.sql.DistributorSql;
import com.sql.PeopleSql;
import com.sql.PrintSql;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Enumeration;
import javax.swing.ImageIcon;
import javax.swing.table.TableColumn;

/**
 *
 * @author Khaled Lela
 */
public class BreadMain extends javax.swing.JFrame {

    /** Creates new form BreadMain */
    public BreadMain(boolean isAdmin) {
        initComponents();
        Toolkit t = Toolkit.getDefaultToolkit();
        Dimension d = t.getScreenSize();
        int x = (d.width - getWidth()) / 2;
        int y = (d.height - getHeight()) / 2;
        setLocation(x, y);
        setIconImage(new ImageIcon(getClass().getResource("/images/icon.png")).getImage());
        personTotalNum = new PeopleSql().countPersonID();
        personIDTF.setText(personTotalNum + 1 + "");
        setTableReder();
        // set Admin authority
        registerButton.setEnabled(isAdmin);
        editButton.setEnabled(isAdmin);
        payFeeds.setEnabled(isAdmin);
        insertDistributor.setEnabled(isAdmin);
        personNameTF.requestFocus();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        peopleTable = new javax.swing.JTable();
        exitButon = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        registerButton = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        personNameTF = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        personIDTF = new javax.swing.JTextField();
        distributorCombo = new javax.swing.JComboBox();
        personNum = new javax.swing.JComboBox();
        categoryTF = new javax.swing.JTextField();
        regionTF = new javax.swing.JTextField();
        clearTFButton = new javax.swing.JButton();
        editButton = new javax.swing.JButton();
        searchButton = new javax.swing.JButton();
        printReport = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        insertDistributor = new javax.swing.JButton();
        payFeeds = new javax.swing.JButton();
        getBreadQuantity = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("تسجيل البيانات");
        setResizable(false);

        peopleTable.setFont(new java.awt.Font("Traditional Arabic", 0, 18));
        peopleTable.setModel(new PeopleTableModel());
        peopleTable.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        peopleTable.setFocusCycleRoot(true);
        peopleTable.setMaximumSize(new java.awt.Dimension(2147483647, 2147483647));
        peopleTable.setRequestFocusEnabled(false);
        peopleTable.setRowHeight(27);
        peopleTable.getTableHeader().setResizingAllowed(false);
        peopleTable.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(peopleTable);

        exitButon.setFont(new java.awt.Font("Traditional Arabic", 0, 18));
        exitButon.setText("تغيير المستخدم");
        exitButon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButonActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "تسجيل بيانات مستفيد جديد من مشروع الخبز", javax.swing.border.TitledBorder.RIGHT, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Traditional Arabic", 0, 18))); // NOI18N

        registerButton.setFont(new java.awt.Font("Traditional Arabic", 0, 18));
        registerButton.setText("تسجيل");
        registerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                registerButtonActionPerformed(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Times New Roman", 0, 18));
        jLabel6.setText("الأســـم");

        personNameTF.setFont(new java.awt.Font("Traditional Arabic", 0, 18));
        personNameTF.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        personNameTF.setNextFocusableComponent(personNum);

        jLabel7.setFont(new java.awt.Font("Times New Roman", 0, 18));
        jLabel7.setText("عدد الأفراد");

        jLabel8.setFont(new java.awt.Font("Times New Roman", 0, 18));
        jLabel8.setText("الفئـــــــــة");

        jLabel9.setFont(new java.awt.Font("Times New Roman", 0, 18));
        jLabel9.setText("المنطقة");

        jLabel10.setFont(new java.awt.Font("Times New Roman", 0, 18));
        jLabel10.setText("الموزع");

        jLabel12.setFont(new java.awt.Font("Times New Roman", 0, 18));
        jLabel12.setText("المسلســـل");

        personIDTF.setFont(new java.awt.Font("Traditional Arabic", 0, 18));
        personIDTF.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        personIDTF.setNextFocusableComponent(personNameTF);

        distributorCombo.setFont(new java.awt.Font("Traditional Arabic", 0, 18));
        distributorCombo.setModel(new DistributorComboModel());
        distributorCombo.setNextFocusableComponent(registerButton);
        distributorCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                distributorComboActionPerformed(evt);
            }
        });

        personNum.setFont(new java.awt.Font("Traditional Arabic", 0, 18));
        personNum.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" }));
        personNum.setNextFocusableComponent(distributorCombo);
        personNum.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                personNumActionPerformed(evt);
            }
        });

        categoryTF.setEditable(false);
        categoryTF.setFont(new java.awt.Font("Simplified Arabic", 0, 18));
        categoryTF.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        regionTF.setEditable(false);
        regionTF.setFont(new java.awt.Font("Traditional Arabic", 0, 18));
        regionTF.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        clearTFButton.setFont(new java.awt.Font("Traditional Arabic", 0, 18));
        clearTFButton.setText("مسح");
        clearTFButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearTFButtonActionPerformed(evt);
            }
        });

        editButton.setFont(new java.awt.Font("Traditional Arabic", 0, 18));
        editButton.setText("تعديل");
        editButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editButtonActionPerformed(evt);
            }
        });

        searchButton.setFont(new java.awt.Font("Traditional Arabic", 0, 18));
        searchButton.setText("بحث");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        printReport.setFont(new java.awt.Font("Traditional Arabic", 0, 18));
        printReport.setText("طباعة");
        printReport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                printReportActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(clearTFButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 66, Short.MAX_VALUE)
                        .addComponent(printReport)
                        .addGap(61, 61, 61)
                        .addComponent(searchButton))
                    .addComponent(distributorCombo, 0, 294, Short.MAX_VALUE)
                    .addComponent(personNameTF, javax.swing.GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE)
                    .addComponent(regionTF, javax.swing.GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel9)
                            .addComponent(jLabel6)
                            .addComponent(jLabel10))
                        .addGap(79, 79, 79)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(categoryTF, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
                            .addComponent(personNum, 0, 131, Short.MAX_VALUE)
                            .addComponent(personIDTF, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE))
                        .addGap(31, 31, 31)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8)
                            .addComponent(jLabel12)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(153, 153, 153)
                        .addComponent(editButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 102, Short.MAX_VALUE)
                        .addComponent(registerButton)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(personIDTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel12))
                        .addGap(14, 14, 14)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(personNum, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(categoryTF, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(3, 3, 3))
                    .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(personNameTF, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(distributorCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(regionTF, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(registerButton)
                        .addComponent(editButton)
                        .addComponent(searchButton)
                        .addComponent(printReport))
                    .addComponent(clearTFButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel2.setFont(new java.awt.Font("Traditional Arabic", 1, 24)); // NOI18N
        jLabel2.setForeground(java.awt.SystemColor.activeCaption);
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText(" بيانـات المستـفيدين من مشـروع الخـبز ");

        insertDistributor.setFont(new java.awt.Font("Traditional Arabic", 0, 18));
        insertDistributor.setText("إضافة موزع");
        insertDistributor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                insertDistributorActionPerformed(evt);
            }
        });

        payFeeds.setFont(new java.awt.Font("Traditional Arabic", 0, 18));
        payFeeds.setText("تسديد الإشتراك");
        payFeeds.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                payFeedsActionPerformed(evt);
            }
        });

        getBreadQuantity.setFont(new java.awt.Font("Traditional Arabic", 0, 18));
        getBreadQuantity.setText("حساب الحصة");
        getBreadQuantity.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                getBreadQuantityActionPerformed(evt);
            }
        });

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon-free-trial.png"))); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 709, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(exitButon)
                        .addGap(116, 116, 116)
                        .addComponent(getBreadQuantity)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 152, Short.MAX_VALUE)
                        .addComponent(payFeeds)
                        .addGap(49, 49, 49)
                        .addComponent(insertDistributor))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(75, 75, 75)
                        .addComponent(jLabel2)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(17, 17, 17)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(insertDistributor)
                    .addComponent(exitButon)
                    .addComponent(payFeeds)
                    .addComponent(getBreadQuantity))
                .addContainerGap())
        );

        jPanel1.getAccessibleContext().setAccessibleName("depostPanel");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exitButonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButonActionPerformed
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Login().setVisible(true);
            }
        });
        dispose();
}//GEN-LAST:event_exitButonActionPerformed
    private void registerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_registerButtonActionPerformed
        personIDTxt = personIDTF.getText();
        name = personNameTF.getText();
        category = categoryTF.getText();

        if (!personIDTxt.equals("") && !name.equals("") && !distributorCombo.getSelectedItem().equals("") && familyNum != 0) {
            personID = Integer.valueOf(personIDTxt);
            // Ensure to increment inseration by only one
            if (personID == personTotalNum + 1) {
                error = new PeopleSql().setPeopleRow(personID, name, familyNum, distributorID, category);
                if (!error) {
                    javax.swing.JOptionPane.showMessageDialog(rootPane, "تم إدخال البيانات بنجاح", "نجاح العملية", javax.swing.JOptionPane.PLAIN_MESSAGE);
                    new DistributorSql().getDistributorQuantity(distributorID);
                    // Clear all s TF process Complete
                    clearTF();
                } else {
                    javax.swing.JOptionPane.showMessageDialog(rootPane, "أسم المستخدم أو المسلسل موجود بالفعل", "فشل العملية", javax.swing.JOptionPane.ERROR_MESSAGE);
                }
            } else {
                javax.swing.JOptionPane.showMessageDialog(rootPane, "رقم المسلسل الصحيح : " + (personTotalNum + 1) + "", "خطأ فى رقم المسلسل ", javax.swing.JOptionPane.ERROR_MESSAGE);
            }
        } else {
            javax.swing.JOptionPane.showMessageDialog(rootPane, "تأكد من إكمال بيانات التسجيل", "ملحوظة", javax.swing.JOptionPane.INFORMATION_MESSAGE);
        }
        personNameTF.requestFocus();
    }//GEN-LAST:event_registerButtonActionPerformed

    private void insertDistributorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insertDistributorActionPerformed
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Distributor().setVisible(true);
            }
        });
    }//GEN-LAST:event_insertDistributorActionPerformed

    private void distributorComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_distributorComboActionPerformed
        name = distributorCombo.getSelectedItem() + "";
        if (!name.equals("")) {
            distributorID = new DistributorSql().getID(name);
            regionTF.setText(new DistributorSql().getRegion(distributorID));
        }
    }//GEN-LAST:event_distributorComboActionPerformed

    private void personNumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_personNumActionPerformed
        familyNum = Integer.valueOf(personNum.getSelectedItem() + "");

        if (familyNum >= 1 && familyNum <= 3) {
            categoryTF.setText("أ");
        } else if (familyNum >= 4 && familyNum <= 7) {
            categoryTF.setText("ب");
        } else if (familyNum >= 8) {
            categoryTF.setText("ج");
        } else {
            categoryTF.setText("");
        }
        distributorCombo.setModel(new DistributorComboModel());
    }//GEN-LAST:event_personNumActionPerformed

    private void clearTFButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearTFButtonActionPerformed
        // Clear all s TF process Complete
        clearTF();
}//GEN-LAST:event_clearTFButtonActionPerformed

    private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed
        personIDTxt = personIDTF.getText();
        // check if person ID exist do Edition
        if (!personIDTxt.equals("")) {
            personID = Integer.valueOf(personIDTxt);
            isIdExist = new PeopleSql().isPersonIDExist(personID);
            if (isIdExist) {
                name = personNameTF.getText();
                String Disname = distributorCombo.getSelectedItem() + "";
                //edit all data Case (1)
                if (!name.equals("") && familyNum != 0 && !Disname.equals("")) {
                    category = categoryTF.getText();
                    error = new PeopleSql().updatePeopleRow(personID, name, familyNum, distributorID, category);
                    editResult(error);
                    //Update Name & Family Number   case(2)
                } else if (!name.equals("") && familyNum != 0) {
                    category = categoryTF.getText();
                    error = new PeopleSql().updatePeopleRow(personID, name, familyNum, category);
                    editResult(error);
                    //Update Name & Distributor ID Case(3)
                } else if (!name.equals("") && !Disname.equals("")) {
                    error = new PeopleSql().updatePeopleRow(personID, name, distributorID);
                    editResult(error);
                    //Update family Num & Distributor ID Case(4)
                } else if (familyNum != 0 && !Disname.equals("")) {
                    category = categoryTF.getText();
                    error = new PeopleSql().updatePeopleRow(personID, familyNum, distributorID, category);
                    editResult(error);
                    //Update Name Only Case(5)
                } else if (!name.equals("")) {
                    error = new PeopleSql().updatePeopleRow(personID, name);
                    editResult(error);
                    //Update family Num only ID Case(6)
                } else if (familyNum != 0) {
                    category = categoryTF.getText();
                    error = new PeopleSql().updatePeopleRow(personID, familyNum, category);
                    editResult(error);
                    //Update  Distributor ID Only Case(7)
                } else if (!Disname.equals("")) {
                    error = new PeopleSql().updatePeopleRow(personID, distributorID);
                    editResult(error);
                } else {
                    javax.swing.JOptionPane.showMessageDialog(rootPane, "تأكد من إدخال المسلسل ثم البيان المراد تعديله", "ملحوظة", javax.swing.JOptionPane.INFORMATION_MESSAGE);
                }
                // if Person ID doesn't exist
            } else {
                javax.swing.JOptionPane.showMessageDialog(rootPane, " رقم المسلسل غير موجود", "فشل العملية", javax.swing.JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_editButtonActionPerformed

    private void editResult(boolean error) {
        if (!error) {
            javax.swing.JOptionPane.showMessageDialog(rootPane, "تم تعديل البيانات بنجاح", "نجاح العملية", javax.swing.JOptionPane.PLAIN_MESSAGE);
            // set Quantity for each Distributor
            new DistributorSql().getDistributorQuantity(distributorID);
            // Clear all s TF process Complete
            clearTF();
        } else {
            javax.swing.JOptionPane.showMessageDialog(rootPane, "خطأ أثناء التعديل حاول مرة أخري", "فشل العملية", javax.swing.JOptionPane.ERROR_MESSAGE);
        }
    }

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        name = personNameTF.getText();
        personIDTxt = personIDTF.getText();
        // check if person ID exist do Edition
        if (!name.equals("")) {
            peopleTable.setModel(new PeopleSearchModel(name));
            setTableReder();
        } else if (!personIDTxt.equals("")) {
            personID = Integer.valueOf(personIDTxt);
            peopleTable.setModel(new PeopleSearchModel(personID));
            setTableReder();
            // Help how to search
        } else {
            javax.swing.JOptionPane.showMessageDialog(rootPane, " البحث يكون برقم المسلسل أو الاسم", "البحث", javax.swing.JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_searchButtonActionPerformed

    private void printReportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_printReportActionPerformed
        name = personNameTF.getText();
        Object distributorName = distributorCombo.getSelectedItem();

        // Search With Distributor ID & Category Case (1)
        if (!distributorName.equals("") && familyNum != 0) {
            //get data [][] for Registered Name
            data = new PrintSql().getPeopleOfDisCategory(distributorName + "", categoryTF.getText());
            String[] columnNames =
                    new String[]{
                "الفئة", "المنطقة", "الموزع", "الأفراد", "الأسم", "مسلسل"
            };
            // Send information to be printed
            new Reports(data, columnNames, " بيانات المسجلين التابعة للموزع : " + distributorName + "  والتابعين للفئة : " + categoryTF.getText());

            // Search With Distributor ID Only (2) 
        } else if (!distributorName.equals("")) {
            // get Data [][] for specify Distributor
            data = new PrintSql().getPeopleOfDisName(distributorName);
            String[] columnNames =
                    new String[]{
                "الفئة", "المنطقة", "الأفراد", "الأسم", "مسلسل"
            };
            // Send information to be printed
            new Reports(data, columnNames, "  بيانات الموزع : " + distributorName);

            // Search  family Num  Only Case (3) >>> SearchSql class
        } else if (familyNum != 0) {
            // get Data [][] for specify family num
            data = new PrintSql().getPeopleOfFamilyNum(familyNum);
            String[] columnNames =
                    new String[]{
                "الفئة", "المنطقة", "الموزع", "الأفراد", "الأسم", "مسلسل"
            };
            // Send information to be printed
            new Reports(data, columnNames, " بيانات المسجلين حسب عدد أفراد العائلة : " + familyNum);
        } else {
            javax.swing.JOptionPane.showMessageDialog(rootPane, "الطباعة تكون عن طريق اسم الموزع أو عدد أفراد العائلة أو الموزع والفئة", "الطباعة", javax.swing.JOptionPane.INFORMATION_MESSAGE);
        }
}//GEN-LAST:event_printReportActionPerformed

    private void payFeedsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_payFeedsActionPerformed
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Feeds().setVisible(true);
            }
        });
    }//GEN-LAST:event_payFeedsActionPerformed

    private void getBreadQuantityActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_getBreadQuantityActionPerformed
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new BreadQuantity().setVisible(true);
            }
        });
    }//GEN-LAST:event_getBreadQuantityActionPerformed
    private void clearTF() {
        personTotalNum = new PeopleSql().countPersonID();
        personIDTF.setText(personTotalNum + 1 + "");
        personNameTF.setText("");
        categoryTF.setText("");
        regionTF.setText("");
        personNum.setSelectedItem("0");
        distributorCombo.setSelectedItem("");
        peopleTable.setModel(new PeopleTableModel());
        setTableReder();
    }

    private void setTableReder() {
        tcol = peopleTable.getColumnModel().getColumns();
        while (tcol.hasMoreElements()) {
            tcol.nextElement().setCellRenderer(new MyCellRenderer());
        }
        peopleTable.getColumnModel().getColumn(1).setPreferredWidth(100);
        peopleTable.getColumnModel().getColumn(2).setPreferredWidth(150);
        peopleTable.getColumnModel().getColumn(4).setPreferredWidth(250);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField categoryTF;
    private javax.swing.JButton clearTFButton;
    private javax.swing.JComboBox distributorCombo;
    private javax.swing.JButton editButton;
    private javax.swing.JButton exitButon;
    private javax.swing.JButton getBreadQuantity;
    private javax.swing.JButton insertDistributor;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton payFeeds;
    private javax.swing.JTable peopleTable;
    private javax.swing.JTextField personIDTF;
    private javax.swing.JTextField personNameTF;
    private javax.swing.JComboBox personNum;
    private javax.swing.JButton printReport;
    private javax.swing.JTextField regionTF;
    private javax.swing.JButton registerButton;
    private javax.swing.JButton searchButton;
    // End of variables declaration//GEN-END:variables
    private int distributorID;
    private int personID;
    private String personIDTxt;
    private String name;
    private int familyNum;
    private String category;
    private boolean error;
    private boolean isIdExist;
    private Object data[][];
    private Enumeration<TableColumn> tcol;
    private int personTotalNum;
}
